home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!news
- From: watzka@stat.uni-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: problem: sun4 and hpux char * difference
- Date: 10 Apr 1996 21:31:01 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4kh9al$drp@sparcserver.lrz-muenchen.de>
- References: <Dpnpn5.Lo3@Cadence.COM>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
- Keywords: Help
-
- tmccoy@nntp.cadence.com (Timothy McCoy) writes:
-
- >I have a program that is made for hpux and sun4 users.
- >The sun program fails and the hpux version does not.
- >The problem stems from a struct definition that includes
- >a pointer to a char. Something like:
-
- >typedef struct record
- > {
- > struct rec *first_rec ;
- > char *name ;
- > } warn_record ;
-
- >After space is allocated for the record, typically, the
- >user will do something like this:
-
- >lrec = (struct record *)calloc((unsigned)1,sizeof(struct record));
- >lrec->node_name = (char *) NULL;
-
- Please note that you cannot be sure that first_rec is a NULL pointer
- if you decide to port this program to another platform but the two
- mentioned in your article. If you do _not_ care about whether the
- pointers are NULL pointers,
-
- lrec = malloc(sizeof *lrec);
-
- should be good enough, and if you want safe and portable NULL pointers,
- you can add
-
- lrec->first_rec = NULL;
- lrec->name = NULL;
-
- >The problem is that, on the sun only, if I try to access
- >any of the standard string functions on this char * the
- >program dies with a segmentation error. e.g.;
-
- >(void) strcpy(astring, lrec->node_name ); /* fine on hp */
- >/* Segmentation fault (core dumped) on sun4 */
-
- >I believe I understand why its dieing on the sun, as
- >a null (zero) address can't be read from (really) but
- >myself and others thought that it was 'standard' to
- >interpret it as zero and (in this example) copy '\0'
- >into astring, which is just what the hp does.
-
- The standard does not define what happens if you pass a
- NULL pointer to a function from the standard library
- that does not accept a NULL pointer. The behaviour
- observed on HP machines is just _one_ possible implementation.
- They try to be helpful by writing an error tolerant
- library.
-
-
- >Could someone shed some light on this for me?
-
- >Additionally, why, Why, WHY does the sun version of
- >this:
-
- >printf("name='%s'\n", lrec->node_name);
-
- >yield this:
-
- >name='(null)'
-
- >and hp yields a more reasonable:
-
- >name=''
-
- In this case, both implementors try to be helpful, but in a
- different way. AFAIK, there is no defined behaviour for
- printf if a "(char *)0" is passed as an argument matching
- a "%s" format string.
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
-
-